package com.example.nonggo.utils;

import android.media.AudioRecord;
import android.util.Base64;
import android.util.Log;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.ref.WeakReference;

public class FileUtils {
    public static String fileToBase64(String filePath) {
        File file = new File(filePath);
        if (!file.exists()) {
            Log.e("FileToBase64Converter", "File does not exist: " + filePath);
            return null;
        }

        try (FileInputStream fileInputStream = new FileInputStream(file);
             ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {

            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = fileInputStream.read(buffer)) != -1) {
                byteArrayOutputStream.write(buffer, 0, bytesRead);
            }

            byte[] fileBytes = byteArrayOutputStream.toByteArray();
            return Base64.encodeToString(fileBytes, Base64.NO_WRAP);


        } catch (IOException e) {
            Log.e("FileToBase64Converter", "Error reading file: " + filePath, e);
            return null;
        }
    }

    public static class AudioWriter implements Runnable {
        private static final String TAG = "AudioWriter";
        private final int BUFFER_SIZE;
        private final File audioFile;
        private final AudioRecord audioRecord;
        private final WeakReference<Thread> recordThread;

        public AudioWriter(int BUFFER_SIZE, File audioFile, AudioRecord audioRecord) {
            this.BUFFER_SIZE = BUFFER_SIZE;
            this.audioFile = audioFile;
            this.audioRecord = audioRecord;
            this.recordThread = new WeakReference<>(new Thread(this));
        }

        private boolean isRecording = false;

        public void startRecording() {
            isRecording = true;
            this.recordThread.get().start();
        }

        public void stopRecording() {
            this.recordThread.get().interrupt();
            isRecording = false;
        }

        @Override
        public void run() {
            FileOutputStream fileOutputStream = null;
            try {
                fileOutputStream = new FileOutputStream(audioFile);
                byte[] buffer = new byte[BUFFER_SIZE];
                while (isRecording) {
                    int read = audioRecord.read(buffer, 0, BUFFER_SIZE);
                    if (AudioRecord.ERROR_INVALID_OPERATION != read) {
                        fileOutputStream.write(buffer);
                    }
                }
            } catch (IOException e) {
                Log.e(TAG, "AudioWriter: ", e);
            } finally {
                if (fileOutputStream != null) {
                    try {
                        fileOutputStream.close();
                    } catch (IOException e) {
                        Log.e(TAG, "AudioWriter: ", e);
                    }
                }
            }
        }
    }
}
